/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.console.cmd;

import java.net.InetAddress;
import java.util.HashMap;

import org.objectweb.fractal.adl.Factory;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.factory.GenericFactory;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
import org.objectweb.fractal.util.Fractal;

import fr.jade.console.core.AbstractCommand;
import fr.jade.console.core.Command;
import fr.jade.console.core.Environment;
import fr.jade.fraclite.adl.FactoryFactory;
import fr.jade.fraclite.orb.registry.NamingService;
import fr.jade.fraclite.orb.registry.Registry;
import fr.jade.fraclite.util.FracLite;
import fr.jade.reflex.api.control.ReflexController;
import fr.jade.reflex.util.Reflex;
import fr.jade.service.nodes.allocator.Allocator;
import fr.jade.util.FractalNavigator;
import fr.jade.util.FractalUtil;

public class SelfInit extends AbstractCommand implements Command {

  public static Component dirty = null;
  
  public String getDescription(){
    return "initialize MiniJade repliquas";
  }
  
  public String getName() {
    return "selfinit";
  }
  
  public String getHelp(){
    return "selfinit <host1> <host2>";
  }
  
  public Object execute(String[] args, Environment env) throws Throwable {

    String host1 = args[1];
    String host2 = args[2];
    
    NamingService ns1 = Registry.getRegistry(host1, 1238);
    NamingService ns2 = Registry.getRegistry(host2, 1238);
    
    Component jadeBoot1 = ns1.lookup("jadeBoot");
    Component jadeBoot2 = ns2.lookup("jadeBoot");
    System.out.println("locate jade instances");
    
    Component jade1 = ns1.lookup("jade");
    Component jade2 = ns2.lookup("jade");
    System.out.println("jade instances connected");
    
    FractalUtil.getNameController(jadeBoot1).setFcName("jadeBoot1");
    FractalUtil.getNameController(jadeBoot1).setFcName("jadeBoot2");
    
    FractalUtil.getNameController(jade1).setFcName("jade1");
    FractalUtil.getNameController(jade2).setFcName("jade2");
    
    System.out.println("jade instances renamed");
    
    Component allocator = null;
    allocator = FractalNavigator.find(jade1, "cluster_manager/allocator", true);
    FractalUtil.getContentController(allocator).addFcSubComponent(jadeBoot2);
    ((Allocator)(allocator.getFcInterface("allocator"))).alloc(null);
    System.out.println("connect jade2 to jade1");
    
    allocator = FractalNavigator.find(jade2, "cluster_manager/allocator", true);
    FractalUtil.getContentController(allocator).addFcSubComponent(jadeBoot1);
    ((Allocator)(allocator.getFcInterface("allocator"))).alloc(null);
    System.out.println("connect jade1 to jade2");
    
//    FractalUtil.getLifeCycleController(jade1).startFc();
//    FractalUtil.getLifeCycleController(jade2).startFc();
    return null;
  }

}
